/**
 * create by young (young2f@outlook.com) on 2020/9/12
 * 差旅登记表
 */


'use strict';
import React from 'react';
import {connect} from 'dva/index';
import {Form, Table} from 'antd';
import styles from '../../../index.less';
import FormBase from '../../../../components/Base/FormBase';
import notify from "../../../../components/NotificationExtra";
import ViewTitle from "../../../../components/ViewTitle";
import {ModalView, SearchForm} from "antd-extra";
import Button from "antd/es/button";
import ModalCreateOrEditReimburse from "../../modal/ModalCreateOrEditReimburse";

const _ = require('lodash');

const table = 'DicReimburse';

@Form.create()
@connect(({
            tenant,
            global,
            dynamicLabel,
            dic
          }) => ({...tenant, ...global, ...dic, ...dynamicLabel}))
export default class XReimburseDataFace extends FormBase {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    this.props.dispatch && this.props.dispatch({
      type: 'global/getModel',
      payload: {table, tenantCode: this.getTenantCode()},
    });
    this.onLoadData().then(data => {
      this.setState(data);
    });
  }

  async onLoadData() {
    const payload = this.props.form.getFieldsValue();
    _.map(payload, (value, key) => {
      if (value === '') {
        payload[key] = undefined;
      }
    });
    const res = await this.props.dispatch({
      type: 'dic/findMany',
      payload: {table,...payload},
    });

    const data = res && res.result ? res.result : [];
    return {data};
  };

  onDelete = record => {
    const {code} = record;
    this.props.dispatch({
      type: 'dic/deleteOne',
      payload: {table, code},
    }).then(res => {
      this.requestReturn(res);
      this.componentDidMount();
    });
  };
  onCreateOrEdit = record => {
    const title = record ? '编辑记录' : "创建记录";
    const type = record ? "dic/updateOne" : "dic/create";
    ModalView.open(ModalCreateOrEditReimburse,
      {
        title,
        width: '1080px',
      }, {
        record, table,
        callback: (payload) => {
          payload.table = table;
          payload.code = record ? record.code : undefined;
          this.props.dispatch({
            type,
            payload,
          }).then(res => {
            this.requestReturn(res);
            this.componentDidMount();
          });
        }
      });
  };
  getSearchFields = () => {
    const model = this.props[`${table}Model`];
    if (!model) return [];
    console.log('model',model);
    const arr = [];
    this.getSearchField(arr, model, "");
    const {records={}} = model;
    this.getSearchField(arr, records.cols, "records.");
    return arr;
  };
  render() {
    const {_notify,form} = this.props;
    if (_notify) {
      notify(_notify);
    }
    const {data} = this.state;
    const model = this.props[`${table}Model`];

    return (
      <Form className={styles.content}>
        <ViewTitle title={'差旅登记表'}
                   actions={<Button type="primary"
                                    onClick={() => this.onCreateOrEdit()}
                   >添加</Button>}
        ></ViewTitle>
        <SearchForm
          style={{marginLeft: '6px', marginRight: '6px'}}
          onSearch={() => {
            this.state.current = 1;
            this.componentDidMount();
          }}
          onReset={this.handleReset}
          cols={4}
          min={8}
          expandText="展开"
          form={form}
        >
          {this.getSearchFields()}
        </SearchForm>


        <Table
          className={styles.antTable}
          bordered={true}
          columns={this.getDicTableCols(model, this.onCreateOrEdit, this.onDelete)}
          dataSource={data}
          scroll={{x: true}}
          pagination={true}
        ></Table>
      </Form>
    );
  }
}
